﻿using AdminSystem.Applications;
using AdminSystem.Domain.User;
using AdminSystem.Repositories;
using AdminSystem.Write.API.Command.FarmsCommand;
using AutoMapper;
using MediatR;
using NPOI.SS.Formula.Functions;

namespace AdminSystem.Write.API.Handers.FarmsHander
{
    public class FarmsAddCommandsHander : IRequestHandler<FarmsAddCommand, ApiResult<object>>
    {
        public FarmsAddCommandsHander(IRepository<Farms> repository, IMapper mapper)
        {
            Repository = repository;
            Mapper = mapper;
        }

        public IRepository<Farms> Repository { get; }
        public IMapper Mapper { get; }
        /// <summary>
        /// 添加养殖场信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(FarmsAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<object> result = new ApiResult<object>();
                result.statusCode = StatusCode.Success;
                result.Message = "添加成功";
                var entity = Mapper.Map<Farms>(request);
                int i = Repository.Insert(entity).Result;
                if (i <= 0)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "添加失败";
                    return Task.FromResult(result);
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
